<?php

/**
 * 
 * @author yuxun
 *
 */
class ServerOpsController extends AdminBaseController {
	
	
	/**
	public function text(){
		set_time_limit(0);
		ob_end_clean();
		ob_implicit_flush();
		$fp = fopen("F:/usr/a.txt", "r")or die("Unable to open file!");
		$nn = 0;
		// 输出单行直到 end-of-file
		while (true){
			//echo '新君威';
			if(!feof($fp)){
				fseek($fp, $nn);//将指针定位到1024字节处
				fpassthru($fp);				
				//echo fgets($fp) . "<br>";
				$nn = ftell($fp);
				//echo $nn. "<br>";
			}else{
				 sleep(1);
				 flush();
				 $fp = fopen("F:/usr/a.txt", "r")or die("Unable to open file!");
				 continue;
			}
			
			if(fgets($fp) == 'true'){
				fclose($myfile);
				break;
			}
		}
	
	}**/
	
	public function index(){
		$this->setTitle('服务器运维管理');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
	
		$filter->addFilter('server_name', 'input', '服务器名称')
		->addFilter('show_status', 'select', '表现状态', array('value' => '', 'options' => array('' => '所有') + ServerService::showStatusList()));
		$filter->addSpecial(true);
		
		$filter->addFilter('begin', 'input', '开服时间', array('date' => 'day'))
		->addFilter('end', 'input', '至', array('date' => 'day'));
	
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = ServerService::opsIndexHeader();
		if(PubFunc::isAjax()){
			$result = ServerService::opsIndexData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=serverOps/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'serverOps/index',
					'length'=>50,
					'page'=>$page
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
	
		}
	}
	
	/**
	 * 开服
	 */
	public function open_server(){
		set_time_limit (0);
		$ids = PubRequest::R('ids');
		
		if($ids == ''){
			$out = array('status'=>0,'msg'=>'请选择要操作的数据','data'=>'');
		}else{
			$out = ServerService::open_server($ids);
		}
		
		$this->out = $out;
		$this->outType = 'json';
	}
	
	/**
	 * 停服
	 */
	public function stop_server(){
		set_time_limit (0);
		$ids = PubRequest::R('ids');
	
		if($ids == ''){
			$out = array('status'=>0,'msg'=>'请选择要操作的数据','data'=>'');
		}else{
			$out = ServerService::stop_server($ids);
		}
	
		$this->out = $out;
		$this->outType = 'json';
	}
	
	/**
	 * 清档
	 */
	public function clear_gear(){
		set_time_limit (0);
		$ids = PubRequest::R('ids');
	
		if($ids == ''){
			$out = array('status'=>0,'msg'=>'请选择要操作的数据','data'=>'');
		}else{
			$out = ServerService::clear_gear($ids);
		}
	
		$this->out = $out;
		$this->outType = 'json';
	}
	
	/**
	 * 表现状态切换为正常状态
	 */
	public function update_show_status(){
		set_time_limit (0);
		$ids = PubRequest::R('ids');
		
		if($ids == ''){
			$out = array('status'=>0,'msg'=>'请选择要操作的数据','data'=>'');
		}else{
			$out = ServerService::update_show_status($ids);
		}
		
		$this->out = $out;
		$this->outType = 'json';
	}
	
	/**
	 * 后端版本更新
	 */
	public function version_update(){
		set_time_limit (0);
		$ids = PubRequest::R('ids');
		$this->out['ids'] = $ids;

		$game = PubBase::getCurrentGame();
		$modServer = new ModServer();
		$list = $modServer->getServerForIds($game,$ids);
		$info = '';
		foreach ($list as $key=>$val){
			$info .= $val['server_name'].',';
		}
		$info = substr($info,0,strlen($info)-1);
		$info .= ' 进行版本更新';		
		$this->out['info'] = $info;
		
		$this->layout = '';
	}
	
	/**
	 * 版本更新（停服）
	 */
	public function version_stop_server(){
		set_time_limit (0);
		$ids = PubRequest::R('ids');
		
		if($ids == ''){
			$out = array('status'=>0,'msg'=>'请选择要操作的数据','data'=>'');
		}else{
			$out = ServerService::version_stop_server($ids);
		}
		
		$this->out = $out;
		$this->outType = 'json';
	}
	
	/**
	 * 版本更新（分发更新包）
	 */
	public function version_hand_out(){
		set_time_limit (0);
		$ids = PubRequest::R('ids');
		
		if($ids == ''){
			$out = array('status'=>0,'msg'=>'请选择要操作的数据','data'=>'');
		}else{
			$out = ServerService::version_hand_out($ids);
		}
		
		$this->out = $out;
		$this->outType = 'json';
	}
	
	/**
	 * 版本更新（检查更新包）
	 */
	public function version_check(){
		set_time_limit (0);
		$ids = PubRequest::R('ids');
	
		if($ids == ''){
			$out = array('status'=>0,'msg'=>'请选择要操作的数据','data'=>'');
		}else{
			$out = ServerService::version_check($ids);
		}
	
		$this->out = $out;
		$this->outType = 'json';
	}
	
	
	/**
	 * 版本更新（更新）
	 */
	public function version_update_oper(){
		set_time_limit (0);
		$ids = PubRequest::R('ids');
	
		if($ids == ''){
			$out = array('status'=>0,'msg'=>'请选择要操作的数据','data'=>'');
		}else{
			$out = ServerService::version_update_oper($ids);
		}
	
		$this->out = $out;
		$this->outType = 'json';
	}
	
	/**
	 * 版本更新（检查版本号）
	 */
	public function version_test(){
		set_time_limit (0);
		$ids = PubRequest::R('ids');
	
		if($ids == ''){
			$out = array('status'=>0,'msg'=>'请选择要操作的数据','data'=>'');
		}else{
			$out = ServerService::version_test($ids);
		}
	
		$this->out = $out;
		$this->outType = 'json';
	}	
	
	/**
	 * 版本更新（开服）
	 */
	public function version_open_server(){
		set_time_limit (0);
		$ids = PubRequest::R('ids');
	
		if($ids == ''){
			$out = array('status'=>0,'msg'=>'请选择要操作的数据','data'=>'');
		}else{
			$out = ServerService::version_open_server($ids);
		}
	
		$this->out = $out;
		$this->outType = 'json';
	}
	
	/**
	 * 版本更新（操作回到上一步）
	 */
	public function version_return(){
		set_time_limit (0);
		$ids = PubRequest::R('ids');
		$cz_status = PubRequest::R('cz_status');
	
		if($ids == ''){
			$out = array('status'=>0,'msg'=>'请选择要操作的数据','data'=>'');
		}else{
			$out = ServerService::version_return($ids,$cz_status);
		}
	
		$this->out = $out;
		$this->outType = 'json';
	}
	
	
	/**
	 * 定时版本更新
	 */
	public function versionFixedTime(){
		$this->setTitle('服务器定时版本更新');
		 
		$filter = new PubFilter();
		$grid   = new PubGrid();
		 
		$filter->addFilter('server_name', 'input', '服务器名称')
		->addFilter('show_status', 'select', '表现状态', array('value' => '', 'options' => array('' => '所有') + ServerService::showStatusList()));
		$filter->addSpecial(true);
		 
		$filter->addFilter('begin', 'input', '开服时间', array('date' => 'day'))
		->addFilter('end', 'input', '至', array('date' => 'day'));
		
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = ServerService::versionFixedTimeHeader();
		if(PubFunc::isAjax()){
			$result = ServerService::versionFixedTimeData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=serverOps/versionFixedTime", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'serverOps/versionFixedTime',
					'length'=>50,
					'page'=>$page,
					'fix' => array(
							'col' => 2
					)
			));
			 
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
			 
		}
	}
	
	public function setVersionTime(){
		if(PubFunc::isAjax()){
			$ids = PubRequest::R('ids');
			$version_update_time = PubRequest::R('version_update_time');
			$update_packet = PubRequest::R('update_packet');
			$out = ServerService::setVersionTime($ids,$version_update_time,$update_packet);
			$this->out = $out;
			$this->outType = 'json';
		}else{
			$this->layout = '';
		
		}

	}
	
	
}